// pages/clock/clock.js
var imageUtil = require('../../utils/util.js');
import { Data } from './clock.data'
var app = getApp();
Page({
  data: Data,
  onPullDownRefresh: function () {
    var that = this;
    getClock(that, 'pullDown');
  },
  onLoad: function (options) {
    var that = this
    var clockid = options.id,
      clocktype = options.type,
      sign = options.sign,
      times = options.times,
      patientid = options.patientid;


    //改变导航头的名称
    if (clocktype == 11 & times == 1) {
      wx.setNavigationBarTitle({
        title: '早间运动'
      })
    }
    if (clocktype == 11 & times == 2) {
      wx.setNavigationBarTitle({
        title: '上午运动'
      })
    }
    if (clocktype == 11 & times == 4) {
      wx.setNavigationBarTitle({
        title: '下午运动'
      })
    }
    if (clocktype == 11 & times == 6) {
      wx.setNavigationBarTitle({
        title: '夜间运动'
      })
    }
    if (clocktype == 9 & times == 7) {
      wx.setNavigationBarTitle({
        title: '月经打卡'
      })
    }
    if (clocktype == 12 & times == 1) {
      wx.setNavigationBarTitle({
        title: '起床时间'
      })
    }
    if (clocktype == 6 & times == 1) {
      wx.setNavigationBarTitle({
        title: '早晨饮水'
      })
    }
    if (clocktype == 6 & times == 2) {
      wx.setNavigationBarTitle({
        title: '上午饮水'
      })
    }
    if (clocktype == 6 & times == 4) {
      wx.setNavigationBarTitle({
        title: '下午饮水'
      })
    }
    if (clocktype == 1 & times == 1) {
      wx.setNavigationBarTitle({
        title: '早餐打卡'
      })
    }
    if (clocktype == 4 & times == 2) {
      wx.setNavigationBarTitle({
        title: '上午加餐'
      })
    }
    if (clocktype == 2 & times == 3) {
      wx.setNavigationBarTitle({
        title: '午餐加餐'
      })
    }
    if (clocktype == 5 & times == 4) {
      wx.setNavigationBarTitle({
        title: '下午加餐'
      })
    }
    if (clocktype == 3 & times == 5) {
      wx.setNavigationBarTitle({
        title: '晚餐打卡'
      })
    }
    if (clocktype == 13 & times == 6) {
      wx.setNavigationBarTitle({
        title: '睡前准备'
      })
    }
    if (clocktype == 12 & times == 6) {
      wx.setNavigationBarTitle({
        title: '睡眠时间'
      })
    }
    if (clocktype == 14 & times == 6) {
      wx.setNavigationBarTitle({
        title: '运动加餐'
      })
    }

    this.setData({ 'clockid': clockid, 'type': clocktype, 'patientid': patientid, "outsign": sign, "outtimes": times });
    getClock(that);
  },

  //跳转首页
  returnbutton: function () {
    wx.switchTab({
      url: '../index/index',//跳转到首页页面tab
    })
  },

  onShareAppMessage: function () {
    if (this.data.type == 1 & this.data.outtimes == 1) {
      var title = '早餐打卡';
    } else if (this.data.type == 2 & this.data.outtimes == 3) {
      var title = '午餐打卡';
    } else if (this.data.type == 3 & this.data.outtimes == 5) {
      var title = '晚餐打卡';
    } else if (this.data.type == 4 & this.data.outtimes == 2) {
      var title = '上午加餐';
    } else if (this.data.type == 5 & this.data.outtimes == 4) {
      var title = '下午加餐';
    } else if (this.data.type == 6) { 
      var title = '喝水打卡';
    } else if (this.data.type == 7) {
      var title = '基础体温打卡'
    } else if (this.data.type == 8) {
      var title = '体重打卡'
    } else if (this.data.type == 12 & this.data.outtimes == 1) {
      var title = '起床打卡'
    } else if (this.data.type == 12 & this.data.outtimes == 6) {
      var title = '睡眠打卡'
    } else if (this.data.type == 13 & this.data.outtimes == 6) {
      var title = '睡前打卡'
    } else if (this.data.type == 14 & this.data.outtimes == 6) {
      var title = '运动加餐'
    }
    return {
      title: this.data.userInfo.realName + ':' + title,
    }
  },


})
//获取打卡信息
function getClock(that, pullDown) {
  if (that.data.type == 6 || that.data.type == 7) {
    var url = '/IbabyWebService/camp/dietHistoryToday?patientid=' + that.data.patientid + '&type=' + that.data.type;
  } else {
    var url = '/IbabyWebService/camp/dietDetail/' + that.data.clockid;
  }
  app.https.get(that, url, {}, function (res) {
    if (that.data.type == 6) {
      var lastdata = JSON.parse(res[0].detail),
        n = res.length,
        total = 0;
      lastdata.food[0].weight = parseInt(lastdata.food[0].weight)
      var createtime = res[0].createtime;
      for (let i = 0; i < n; i++) {
        let data = JSON.parse(res[i].detail)
        total = total + parseInt(data.food[0].weight);
      }
      that.setData({
        'total': total,
        'lastdata': lastdata,
        'out.createtime': createtime
      })
    } else if (that.data.type == 7) {
      var lastdata = JSON.parse(res[0].detail);
      var createtime = res[0].createtime;
      that.setData({ 'lastdata': lastdata, 'out.createtime': createtime })
    } else if (that.data.type == 8) {
      var lastdata = JSON.parse(res.detail);
      var createtime = res.createtime;
      that.setData({ 'lastdata': lastdata, 'out.createtime': createtime });
    } else {

      var data = {};
      data['out'] = res;

      that.setData(data);

      var t = JSON.parse(data['out'].detail);

      console.log(t)

      data['out'].detail = [] = t;
      that.setData(data);
      wx.getImageInfo({
        src: res.img,
        success: function (res) {
          var imageSize = imageUtil.imageUtil(res);
          data['imageheight'] = imageSize.imageHeight;
          data['imagewidth'] = imageSize.imageWidth;
          that.setData(data);
        }
      })
      // console.log(res)
    }
    // 获取用户信息
    app.https.get(that, '/IbabyWebService/Manage/AppUser/brief?patientid=' + that.data.patientid, {}, function (rel) {
      // console.log(rel)
      var data = {};
      data['userInfo'] = rel;
      that.setData(data);
    }, 'pullDown')
  }, pullDown)
}